BSEARCH

Section: MINTLIB LIBRARY FUNCTIONS (3)
Updated: 3 March 1993
Index Return to Main Contents
 

NAME

bsearch - binary search a sorted table  

SYNOPSIS

#include <stdlib.h>

void *bsearch(const void *key, const void *base, 
              size_t total_elems, size_t elem_size, 
              int (*compare)(const void *one, const void *two));
 

DESCRIPTION

bsearch is a binary search routine generalized from Knuth (6.2.1) Algorithm B. It returns a pointer into a table indicating where a datum may be found. The table must be previously sorted in increasing order according to a provided comparison function.

  - key points to a datum instance to be sought in
    the table.
  - base points to the element at the base of the table.
  - total_elems is the number of elements in the table.
  - elem_size is the size, in bytes, of each element
    in the table.
  - compare is the name of the comparison function,
    which is called with two arguments that point to
    the elements being compared. As the function must
    return an integer less than, equal to, or greater
    than zero, so must the first argument to be considered
    be less than, equal to, or greater than the second.  

EXAMPLE

The example below searches a table containing pointers to
nodes consisting of a string and its length. The table is
ordered alphabetically on the string in the node pointed
to by each entry.

This code fragment reads in strings and either finds the
corresponding node, in which case it prints out the string
and its length, or it prints an error message.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TABSIZE 1000
    struct node /* These are stored in the table */
    {
      char *string;
      int length;
    };

    struct node table[TABSIZE]; /* Table to be searched */
          .
          .
          .
    {
      struct node *node_ptr, node;
      int node_compare(const struct *node1, const struct *node2);
      char str_space[20]; /* space to read string into */
      .
      .
      .
      node.string = str_space;
      while (scanf("%s", node.string) != EOF)
      {
        node_ptr = (struct node *)bsearch(&node, table, TABSIZE,
                   sizeof(struct node), node_compare);
        if (node_ptr != NULL)
          printf("string = %20s, length = %dn", 
                 node_ptr->string, node_ptr->length);
        else
          printf("not found: %sn", node.string);
      }
    }

    /* Compare two nodes based on an alphabetical */
    /* ordering of the string field.              */
    int node_compare(const void *table_node, const void *key_node)
    {
      const struct node *table = (const struct node *)table_node;
      const struct node *key = (const struct node *)key_node;
      
      return(strcmp(table->string, key->string));
    }
 

SEE ALSO

qsort(3)  

NOTES

The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. A NULL pointer is returned if the key cannot be found in the table.
 

Index

NAME
SYNOPSIS
DESCRIPTION
EXAMPLE
SEE ALSO
NOTES

This document was created by man2html, using the manual pages.
Time: 11:14:45 GMT, June 22, 2025